#include <iostream>
#include <leveldb/db.h>
#include <string>
#include <memory>

#define DB_PATH "/tmp/leveldb"

int main(int argc, char **argv)
{
	std::cout << DB_PATH << std::endl;
	leveldb::Options opts;
	opts.create_if_missing = true;

	leveldb::DB *dbptr = nullptr;

	leveldb::Status status = leveldb::DB::Open(opts, DB_PATH, &dbptr);
	if (status.ok())
	{
		std::cout << "leveldb open success" << std::endl;
	}
	else
	{
		std::cout << "failed to open db" << status.ToString() << std::endl;
	}

	std::unique_ptr<leveldb::DB> safedb{dbptr};

	leveldb::WriteOptions write_opt;
	// if not sync, write data to system buffer, but leveldb is memory db
	write_opt.sync = false;
	// add data
	status = safedb->Put(write_opt, "hello", "leveldb");
	status = safedb->Put(write_opt, "time", "20241101");

	// query data
	leveldb::ReadOptions read_opt;
	read_opt.verify_checksums = false;

	std::string res{};
	status = safedb->Get(read_opt, "hello", &res);

	if (status.ok())
	{
		std::cout << "query: " << res << std::endl;
	}
	else if (status.IsNotFound())
	{
		std::cout << "query ok but value not found" << res << std::endl;
	}

	// delete data
	status = safedb->Delete(write_opt, "hello");

	// update data
	status = safedb->Put(write_opt, "hello", "fucker fucker");
	status = safedb->Put(write_opt, "hello", "etcix_admin");
	status = safedb->Get(read_opt, "hello", &res);
	std::cout << res << std::endl;
	return 0;
}
